package cn.edu.sctu.sctu_lysd.view.act;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.text.ParseException;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.bean.TouristMessageBean;
import cn.edu.sctu.sctu_lysd.biz.TouristMessageBiz;
import cn.edu.sctu.sctu_lysd.databinding.ActivityTouristMessageBinding;
import cn.edu.sctu.sctu_lysd.my_view.DateChooseWheelViewDialog;
import cn.edu.sctu.sctu_lysd.presenter.TouristMessagePresenter;
import cn.edu.sctu.sctu_lysd.utils.DateUtils;
import cn.edu.sctu.sctu_lysd.utils.LightUtils;
import cn.edu.sctu.sctu_lysd.utils.ProgressUtil;

/**
 * Created by wangfuxin on 2017-04-09.
 */

public class TouristMessageAct extends BaseActivity implements TouristMessageBiz.TouristMessageView {

    private ActivityTouristMessageBinding mBinding;

    private TouristMessageBiz.Presenter mPresenter;

    private String originPre;//起始点
    private String originDetail;//起始点 详细
    private String destinationPre;//终点地址
    private String destinationDetail;//终点地址

    private final int START_TIME_FLAG = 0;
    private final int END_TIME_FLAG = 1;

    private String mStartTime = "";
    private String mEndTime = "";

    private TouristMessageBean touristMessageBean = null;

    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    private IndexPlacePopWin placePopWin;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_tourist_message;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityTouristMessageBinding) bind;

        progressUtil = ProgressUtil.getInstance(this);

        mBinding.startTimeTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickTime(START_TIME_FLAG);
            }
        });

        mBinding.touristDestinationPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWin(mBinding.touristDestinationPre);
            }
        });

        mBinding.endTimeTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickTime(END_TIME_FLAG);
            }
        });

        initPresenter(new TouristMessagePresenter(this, this));

        getIntentData();

    }

    @Override
    public void initPresenter(TouristMessageBiz.Presenter presenter) {

        this.mPresenter = presenter;

        mPresenter.start();
    }

    @Override
    public void uploadSuccess() {

        progressUtil.dismiss(progressDialog);

        Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void uploadFailed() {

        progressUtil.dismiss(progressDialog);

        Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();

    }

    private void getIntentData() {

        Bundle extras = getIntent().getExtras();

        originPre = extras.getString(IndexActivity.ORIGIN_PRE);
        originDetail = extras.getString(IndexActivity.ORIGIN_DETAIL);

    }

    public void pickTime(final int flag) {
        DateChooseWheelViewDialog startDateChooseDialog = new DateChooseWheelViewDialog(this,
                new DateChooseWheelViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        switch (flag) {

                            case START_TIME_FLAG:

                                mStartTime = time;

                                mBinding.startTimeTxt.setText(time);

                                break;

                            case END_TIME_FLAG:

                                mEndTime = time;

                                mBinding.endTimeTxt.setText(time);

                                break;
                            default:

                                break;

                        }

                    }
                });

        startDateChooseDialog.setDismissListener(new DateChooseWheelViewDialog.DismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        startDateChooseDialog.showDateChooseDialog();
    }

    private void showPopWin(final View view){

        LightUtils.lightOff(this);

        View contentView = LayoutInflater.from(this).inflate(R.layout.pup_win_place, null);

        placePopWin = new IndexPlacePopWin(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true,
                this,view);

        ImageView back =  (ImageView) contentView.findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                placePopWin.dismiss();
            }
        });

        placePopWin.setSelectOver(new IndexPlacePopWin.selectOver() {
            @Override
            public void selectOver(String place) {

                /**
                 * 通知 View 显示
                 */

                mBinding.touristDestinationPre.setText(place);

                placePopWin.dismiss();

            }
        });

        placePopWin.showAsDropDown();

        placePopWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

                LightUtils.lightOn(TouristMessageAct.this);
            }
        });

    }


    public void back(View view) {
        finish();

    }

    public void touristMessageSubmit(View view) {

        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_HORIZONTAL, false, "sss");

        try {

            combinationData();

            mPresenter.uploadMessage(touristMessageBean);

        } catch (TouristMessageError touristMessageError) {
            touristMessageError.printStackTrace();

            progressUtil.dismiss(progressDialog);

            Toast.makeText(this, touristMessageError.getMessage(), Toast.LENGTH_SHORT).show();

        } catch (ParseException e) {
            e.printStackTrace();

            progressUtil.dismiss(progressDialog);

            Toast.makeText(TouristMessageAct.this, "信息填写不完整，请检查", Toast.LENGTH_LONG).show();
        }


    }

    private void combinationData() throws TouristMessageError, ParseException {

        if (touristMessageBean == null)
            touristMessageBean = new TouristMessageBean();

        destinationPre = mBinding.touristDestinationPre.getText().toString();
        destinationDetail = mBinding.touristDestinationDetail.getText().toString();

        if (mStartTime.equals("")) throw new TouristMessageError("时间填写不完整");
        if (mEndTime.equals("")) throw new TouristMessageError("时间填写不完整");
        if (destinationPre.equals("")) throw new TouristMessageError("时间填写不完整");
        if (destinationDetail.equals("")) throw new TouristMessageError("时间填写不完整");

        touristMessageBean.setOriginPre(originPre);
        touristMessageBean.setOriginDetail(originDetail);
        touristMessageBean.setDestinationPre(destinationPre);
        touristMessageBean.setDestinationDetail(destinationPre);
        touristMessageBean.setStartTime(DateUtils.strToDate(mStartTime, "yyyy/MM/dd HH:mm"));
        touristMessageBean.setEndTime(DateUtils.strToDate(mStartTime, "yyyy/MM/dd HH:mm"));

    }

    class TouristMessageError extends Exception {

        public TouristMessageError(String message) {
            super(message);
        }
    }

}
